<template>
  <div class="container">
    <div class="page-header">
      <div class="back-button">
        <el-button
          @click="$router.push({ name: 'Sanjianquan' })"
          icon="el-icon-back"
          >返回</el-button
        >
      </div>
      <h1>三级安全教育/制度教育/技能教育培训卡列表</h1>
    </div>

    <div class="main-content">
      <div class="content-wrapper">
        <div class="search-bar">
          <el-form :inline="true" :model="searchForm">
            <el-form-item label="姓名">
              <el-input
                v-model="searchForm.name"
                placeholder="请输入姓名"
              ></el-input>
            </el-form-item>
            <el-form-item label="班组">
              <el-input
                v-model="searchForm.team"
                placeholder="请输入班组"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                @click="handleSearch"
                icon="el-icon-search"
                >查询</el-button
              >
              <el-button @click="resetSearch" icon="el-icon-refresh"
                >重置</el-button
              >
              <el-button type="primary" @click="handleAdd" icon="el-icon-plus"
                >新增</el-button
              >
            </el-form-item>
          </el-form>
        </div>

        <div style="padding: 20px;background-color: #FFF;border-radius: 5px;">
          <el-table
            :data="tableData"
            border
            style="width: 100%"
            v-loading="loading"
          >
            <el-table-column
              prop="name"
              label="姓名"
              width="120"
            ></el-table-column>
            <el-table-column
              prop="team"
              label="班组"
              width="120"
            ></el-table-column>
            <el-table-column
              prop="idCard"
              label="身份证号码"
              width="250"
            ></el-table-column>
            <el-table-column
              prop="jobType"
              label="工种"
              width="120"
            ></el-table-column>
            <el-table-column
              prop="entryDate"
              label="入职日期"
              width="120"
            ></el-table-column>
            <el-table-column
              prop="cardCreateDate"
              label="建卡日期"
              width="120"
            ></el-table-column>
            <el-table-column label="操作" width="300" fixed="right">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="info"
                  @click="handleView(scope.row)"
                  icon="el-icon-view"
                  >查看</el-button
                >
                <el-button
                  size="mini"
                  type="primary"
                  @click="handleEdit(scope.row)"
                  icon="el-icon-edit"
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row)"
                  icon="el-icon-delete"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>

        <div class="pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.current"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getPage, removeById } from "@/api/safety-training-card";

export default {
  data() {
    return {
      loading: false,
      searchForm: {
        name: "",
        team: "",
      },
      page: {
        current: 1,
        size: 10,
        total: 0,
      },
      tableData: [],
    };
  },
  created() {
    this.loadData();
  },
  methods: {
    async loadData() {
      this.loading = true;
      try {
        const { data } = await getPage({
          current: this.page.current,
          size: this.page.size,
          name: this.searchForm.name,
          team: this.searchForm.team,
        });
        this.tableData = data.data.records;
        this.page.total = data.data.total;
      } catch (error) {
        this.$message.error("加载数据失败");
      } finally {
        this.loading = false;
      }
    },
    handleSearch() {
      this.page.current = 1;
      this.loadData();
    },
    resetSearch() {
      this.searchForm = {
        name: "",
        team: "",
      };
      this.handleSearch();
    },
    handleSizeChange(val) {
      this.page.size = val;
      this.loadData();
    },
    handleCurrentChange(val) {
      this.page.current = val;
      this.loadData();
    },
    handleAdd() {
      this.$router.push({
        name: "Sanjianquan",
        query: { type: "add" },
      });
    },
    handleView(row) {
      this.$router.push({
        name: "Sanjianquan",
        query: { id: row.id, type: "view" },
      });
    },
    handleEdit(row) {
      this.$router.push({
        name: "Sanjianquan",
        query: { id: row.id, type: "edit" },
      });
    },
    async handleDelete(row) {
      try {
        await this.$confirm("确认删除该记录?", "提示", {
          type: "warning",
        });
        await removeById(row.id);
        this.$message.success("删除成功");
        this.loadData();
      } catch (error) {
        if (error !== "cancel") {
          this.$message.error("删除失败");
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: calc(100vh - 40px);
  padding: 20px;
  display: flex;
  flex-direction: column;

  .page-header {
    padding: 16px 20px;
    background-color: #fff;
    border-bottom: 1px solid #dcdfe6;
    display: flex;
    align-items: center;
    width: calc(1200px - 40px);
    max-width: 100%;
    margin: 0 auto;
    border-radius: 5px;

    .back-button {
      margin-right: 20px;
    }

    h1 {
      margin: 0;
      font-size: 20px;
      font-weight: 500;
      color: #303133;
    }
  }

  .main-content {
    flex: 1;
    padding: 20px;
    // background-color: #f5f7fa;
    overflow: auto;
    display: flex;
    justify-content: center;

    .content-wrapper {
      width: 1200px;
      max-width: 100%;
    }
  }
}
.el-form-item {
  margin-bottom: 0px;
}
.search-bar {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.el-table {
  // margin-top: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination {
  margin-top: 20px;
  text-align: right;
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
</style> 